package com.zhaolang.qogir.ui.manage;

import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.suke.widget.SwitchButton;
import com.zhaolang.qogir.R;
import com.zhaolang.qogir.ui.ActivityTabs;
import com.zhaolang.qogir.ui.BaseFragment;
import com.zhaolang.qogir.ui.TitleView;
import com.zhaolang.qogir.ui.WelcomePre;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;

public class FragmentManage extends BaseFragment implements View.OnClickListener {
    private TitleView mTitle;
    private View rootView;
    private ViewPager mViewPaper;

    private List<ImageView> images;
    private List<View> dots;
    private int currentItem;
    private ImageView iv_manage_product;
    //记录上一次点的位置
    private int oldPosition = 0;
    //存放图片的id
    private int[] imageIds = new int[]{
            R.drawable.banner_default,
            R.drawable.splash_picture,
            R.drawable.banner_default,
    };

    private LinearLayout manage_action_decorate, manage_action_product, manage_action_comment, manage_action_wallet;

    private TextView title;
    private ViewPagerAdapter adapter;
    private ScheduledExecutorService scheduledExecutorService;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        super.onCreateView(container, savedInstanceState);
        if (rootView == null) {
            rootView = inflater.inflate(R.layout.fragment_manage, null);
            init_view();
        }
        ViewGroup parent = (ViewGroup) rootView.getParent();
        if (parent != null) {
            parent.removeView(rootView);
        }
        return rootView;
    }

    private void init_view() {
        View title = rootView.findViewById(R.id.vg_title);
        mTitle = new TitleView(getContext(), title);
        mTitle.setCenterText(this.getResources().getText(R.string.title_main_manage), this);
        mTitle.setTextColor(mContext.getResources().getColor(R.color.white));

        mViewPaper = rootView.findViewById(R.id.view_pager);

        iv_manage_product = rootView.findViewById(R.id.iv_manage_banner_product);

        manage_action_decorate = rootView.findViewById(R.id.manage_action_decorate);
        manage_action_product = rootView.findViewById(R.id.manage_action_product);
        manage_action_comment = rootView.findViewById(R.id.manage_action_comment);
        manage_action_wallet = rootView.findViewById(R.id.manage_action_wallet);

        manage_action_decorate.setOnClickListener(this);
        manage_action_product.setOnClickListener(this);
        manage_action_comment.setOnClickListener(this);
        manage_action_wallet.setOnClickListener(this);

        //显示的图片
        images = new ArrayList<ImageView>();
        for (int i = 0; i < imageIds.length; i++) {
            ImageView imageView = new ImageView(getContext());
            imageView.setBackgroundResource(imageIds[i]);
            images.add(imageView);
        }

        //显示的小点
        dots = new ArrayList<View>();
        dots.add(rootView.findViewById(R.id.dot_0));
        dots.add(rootView.findViewById(R.id.dot_1));
        dots.add(rootView.findViewById(R.id.dot_2));
        dots.add(rootView.findViewById(R.id.dot_3));
        dots.add(rootView.findViewById(R.id.dot_4));

        adapter = new ViewPagerAdapter();
        mViewPaper.setAdapter(adapter);

        mViewPaper.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageSelected(int position) {
                dots.get(position).setBackgroundResource(R.drawable.dot_focused);
                dots.get(oldPosition).setBackgroundResource(R.drawable.dot_normal);

                oldPosition = position;
                currentItem = position;
            }

            @Override
            public void onPageScrolled(int arg0, float arg1, int arg2) {

            }

            @Override
            public void onPageScrollStateChanged(int arg0) {

            }
        });
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.manage_action_decorate:
                break;
            case R.id.manage_action_comment:
                startActivity(new Intent(mContext, ActivityManageAllComment.class));
                break;
            case R.id.manage_action_wallet:
                break;
            case R.id.manage_action_product:
                startActivity(new Intent(mContext, ActivityManageShopClassify.class));
                break;
            default:
                break;
        }
    }

    /**
     * 自定义Adapter
     *
     * @author liuyazhuang
     */
    private class ViewPagerAdapter extends PagerAdapter {

        @Override
        public int getCount() {
            return images.size();
        }

        @Override
        public boolean isViewFromObject(View arg0, Object arg1) {
            return arg0 == arg1;
        }

        @Override
        public void destroyItem(ViewGroup view, int position, Object object) {
            // TODO Auto-generated method stub
//			super.destroyItem(container, position, object);
//			view.removeView(view.getChildAt(position));
//			view.removeViewAt(position);
            view.removeView(images.get(position));
        }

        @Override
        public Object instantiateItem(ViewGroup view, int position) {
            // TODO Auto-generated method stub
            view.addView(images.get(position));
            return images.get(position);
        }
    }

    /**
     * 利用线程池定时执行动画轮播
     */
    @Override
    public void onStart() {
        // TODO Auto-generated method stub
        super.onStart();
        scheduledExecutorService = Executors.newSingleThreadScheduledExecutor();
        scheduledExecutorService.scheduleWithFixedDelay(
                new ViewPageTask(),
                2,
                2,
                TimeUnit.SECONDS);
    }

    /**
     * 图片轮播任务
     *
     * @author liuyazhuang
     */
    private class ViewPageTask implements Runnable {

        @Override
        public void run() {
            currentItem = (currentItem + 1) % imageIds.length;
            mHandler.sendEmptyMessage(0);
        }
    }

    /**
     * 接收子线程传递过来的数据
     */
    private Handler mHandler = new Handler() {
        public void handleMessage(android.os.Message msg) {
            mViewPaper.setCurrentItem(currentItem);
        }

        ;
    };


    @Override
    public void onStop() {
        super.onStop();
        if (scheduledExecutorService != null) {
            scheduledExecutorService.shutdown();
            scheduledExecutorService = null;
        }
    }
}
